package com.green.common.util.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * XML工具类. 转换规则： 1.主要是Map与List的互相嵌套 2.同名称的节点会被装进List
 * 
 * @author lj
 */
public class Xml2MapUtils {

	protected static final Logger LOGGER = LoggerFactory.getLogger(Xml2MapUtils.class);

	/**
	 * xml字符串转换为Map.
	 * 
	 * @param xmlStr
	 * @return
	 */
	public static Map<String, Object> xmlStr2Map(String xmlStr) {
		Document document = xmlStr2XmlDocument(xmlStr);
		return dom2Map(document);

	}

	/**
	 * 
	 * XML document转换为Map.
	 * 
	 * @param doc
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static Map<String, Object> dom2Map(Document doc) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (doc == null) {
			return map;
		}
		// 获取根元素
		Element root = doc.getRootElement();
		for (Iterator iterator = root.elementIterator(); iterator.hasNext();) {
			// 获取第一层子元素
			Element element = (Element) iterator.next();
			// 获取第二层子元素
			List list = element.elements();
			if (list.size() > 0) {
				map.put(element.getName(), elements2Map(element));
			} else {
				map.put(element.getName(), element.getTextTrim());
			}
		}
		return map;
	}

	/**
	 * XML element转换为Map.
	 * 
	 * @param element
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static Map<String, Object> elements2Map(Element element) {
		Map<String, Object> map = new HashMap<String, Object>();
		List list = element.elements();
		int size = list.size();
		if (size > 0) {
			for (int i = 0; i < size; i++) {
				Element e = (Element) list.get(i);
				List<Object> mapList = new ArrayList<Object>();
				if (e.elements().size() > 0) {
					// 迭代获取子元素
					Map<String, Object> m = elements2Map(e);
					if (map.get(e.getName()) != null) {
						Object obj = map.get(e.getName());
						if (obj instanceof ArrayList) {
							mapList = (List<Object>) obj;
							mapList.add(m);
						} else {
							mapList = new ArrayList<Object>();
							mapList.add(obj);
							mapList.add(m);
						}
						map.put(e.getName(), mapList);
					} else {
						map.put(e.getName(), m);
					}
				} else {
					if (map.get(e.getName()) != null) {
						Object obj = map.get(e.getName());
						if (obj instanceof ArrayList) {
							mapList = (List<Object>) obj;
							mapList.add(e.getTextTrim());
						} else {
							mapList = new ArrayList<Object>();
							mapList.add(obj);
							mapList.add(e.getTextTrim());
						}
						map.put(e.getName(), mapList);
					} else {
						map.put(e.getName(), e.getTextTrim());
					}
				}
			}
		} else {
			map.put(element.getName(), element.getTextTrim());
		}
		return map;
	}

	/**
	 * xml字符串转为 XmlDocument.
	 * 
	 * @param xmlStr
	 * @return
	 */
	public static Document xmlStr2XmlDocument(String xmlStr) {
		Document document = null;
		try {
			document = DocumentHelper.parseText(xmlStr);
		} catch (DocumentException e) {
			LOGGER.error("xml字符串转为 XmlDocument", e);
		}
		return document;
	}

	/**
	 * xmlDocument 转为 xml字符串.
	 * 
	 * @param document
	 * @return
	 */
	public static String xmlDocument2XmlStr(Document document) {
		return document.asXML();
	}
}
